package com.yanqu.road.dao.impl.activity.civil;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.civil.ActivityReceiveCivilRewardData;
import com.yanqu.road.entity.activity.climbtower.ClimbTowerTopData;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ActivityCivilRewardDaoImpl extends TempDataObjectDao<ActivityReceiveCivilRewardData> {

    public static String selectSql = "select * from `t_u_activity_civil_receive_reward` where `activityId` = ? and `userId` = ?";
    public Map<Long, Map<Integer, ActivityReceiveCivilRewardData>> getData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, ActivityReceiveCivilRewardData>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ActivityReceiveCivilRewardData data = parse(rs);
                Map<Integer, ActivityReceiveCivilRewardData> conditionMap = result.computeIfAbsent(data.getActivityUserId(), k -> new ConcurrentHashMap<>());
                conditionMap.put(data.getConditionId(), data);
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String insertSql = "INSERT INTO `t_u_activity_civil_receive_reward`(`userId`, `activityId`, `activityUserId`, `conditionId`, `createTime`) VALUES (?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(ActivityReceiveCivilRewardData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getActivityUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getConditionId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected boolean updateData(ActivityReceiveCivilRewardData data) {
        return false;
    }

    @Override
    protected ActivityReceiveCivilRewardData parseData(ResultSet rs) throws SQLException {
        ActivityReceiveCivilRewardData data = new ActivityReceiveCivilRewardData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setConditionId(rs.getInt("conditionId"));
        data.setActivityUserId(rs.getLong("activityUserId"));
        data.setCreateTime(rs.getLong("createTime"));
        return data;
    }
}
